import java.util.HashSet;
import java.util.Set;

public class Solution547 {
    public int findCircleNum(int[][] isConnected) {
        int[] b=new int[isConnected.length];
        for (int i=0;i<b.length;i++){
            b[i]=i;
        }
        for (int i=0;i<isConnected.length;i++){
            for (int j=i+1;j<isConnected.length;j++){
                if (isConnected[i][j]==1){
                    int x=find(b,i);
                    int y=find(b,j);
                    b[x]=y;
                }
            }
        }
        Set<Integer> set=new HashSet<>();
        for (int i=0;i<b.length;i++){
            set.add(find(b,i));
        }
        return set.size();
    }

    int find(int[] b,int t){
        while (b[t]!=t){
            t=b[t];
        }
        return t;
    }

    public static void main(String[] args) {
        System.out.println(new Solution547().findCircleNum(new int[][]{{1,1,0},{1,1,0},{0,0,1}}));
    }
}
